package com.abel.bigwater.data.kente

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.data.mapper.DataMapper
import com.abel.bigwater.data.mapper.RtuMapper
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner

@SpringBootTest
@RunWith(SpringRunner::class)
class KenteTcpHandlerTest {
    @Autowired
    var rtuMapper: RtuMapper? = null

    @Autowired
    var dataMapper: DataMapper? = null

    var firmId = "23"

    var firmCode = "lt-"

    var firmName = "测试"

    @Test
    fun storeData() {
        val tcp = KenteTcpHandler(dataMapper!!, rtuMapper!!, firmId, firmCode)
        val ch = EmbeddedChannel(KenteDecoder(), tcp)

        ch.writeInbound(Unpooled.buffer().writeBytes(BcdString.toByteArray("""
            41 54 2b 44 61 74 31 3d 19 7e cb 32 01 d9 14 34 01 34 ab 02 00 63 5d ab 43 63 62 95 3e 00 e9 41
3e 2d a3 12 00 2d 0d 00 00 35 13 27 04 00 00 00 00 00 00 00 00 00 00 c4 93 c8 89 be bb d6 ac 9c
40 20 03 12 00 00 00 43 81 7e cb 32 01 d9 14 34 01 98 ab 02 00 8f d8 ab 43 82 65 95 3e 65 7d 42
3e 33 a3 12 00 2d 0d 00 00 a1 06 27 04 00 00 00 00 00 00 00 00 00 00 c4 93 3a fe cb bb 73 1d 9d
40 20 03 12 00 00 00 12 7e 7e cb 32 01 d9 14 34 01 fc ab 02 00 a9 0b aa 43 b4 08 96 3e c6 61 40
3e 38 a3 12 00 2d 0d 00 00 d2 1f 27 04 00 00 00 00 00 00 00 00 00 00 c4 93 56 85 c8 bb 0f 78 9b
40 20 03 12 00 00 00 16 42 7e cb 32 01 d9 14 34 01 60 ac 02 00 07 4d 9b 43 c7 c7 95 3e 45 ad 2f
3e 3e a3 12 00 2d 0d 00 00 3d 08 27 04 00 00 00 00 00 00 00 00 00 00 c4 93 72 6c b5 bb 96 28 8e
40 20 03 12 00 00 00 07 26 7e cb 32 01 d9 14 34 01 c4 ac 02 00 b5 a6 9c 43 3c ba 95 3e f8 5c 31
3e 43 a3 12 00 2d 0d 00 00 30 11 27 04 00 00 00 00 00 00 00 00 00 00 c4 93 c8 59 c6 bb 0a 39 8f
40 20 03 12 00 00 00 35 5d 7e cb 32 01 d9 14 34 01 28 ad 02 00 bb 18 a7 43 86 c7 94 3e 5c 1d 3d
3e 48 a3 12 00 2d 0d 00 00 61 22 27 04 00 00 00 00 00 00 00 00 00 00 c4 93 72 f4 c8 bb d8 c5 98
40 20 03 12 00 00 00 25 09
        """.trimIndent())))
        Thread.sleep(2 * 1000)
    }

    @Test
    fun storeDataMeter() {
        val tcp = KenteTcpHandler(dataMapper!!, rtuMapper!!, firmId, firmCode)
        val ch = EmbeddedChannel(KenteDecoder(), tcp)

        ch.writeInbound(Unpooled.buffer().writeBytes(BcdString.toByteArray("""
            41 54 2b 44 61 74 61 3d 1d 64 81 a9 00 d9 14 34 01 f4 01 00 00 00 00 00 00 f9 89 53 3e 00 00 00
00 9b 18 00 00 00 00 00 00 ec 00 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 95 31 64 81 a9 00 d9 14 34 01 e8 03 00 00 00 00 00 00 1b a9 68 3e 00 00 00
00 9b 18 00 00 00 00 00 00 46 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 5e e3 64 81 a9 00 d9 14 34 01 dc 05 00 00 00 00 00 00 5e bf 85 3e 00 00 00
00 9b 18 00 00 00 00 00 00 46 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 99 4a 64 81 a9 00 d9 14 34 01 d0 07 00 00 00 00 00 00 9e ee 8b 3e 00 00 00
00 9b 18 00 00 00 00 00 00 46 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 0b 45 64 81 a9 00 d9 14 34 01 c4 09 00 00 00 00 00 00 58 e2 8d 3e 00 00 00
00 9b 18 00 00 00 00 00 00 46 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 1f 11 64 81 a9 00 d9 14 34 01 b8 0b 00 00 00 00 00 00 6f 05 91 3e 00 00 00
00 9b 18 00 00 00 00 00 00 46 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 ba 36 64 81 a9 00 d9 14 34 01 ac 0d 00 00 00 00 00 00 a6 8f 93 3e 00 00 00
00 9b 18 00 00 00 00 00 00 46 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 97 81 64 81 a9 00 d9 14 34 01 a0 0f 00 00 31 8c 9a 41 6b de 85 3e dd e9 2e
40 9c 18 00 00 00 00 00 00 7e 00 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 2e 3a 64 81 a9 00 d9 14 34 01 94 11 00 00 a3 80 9e 41 f2 fd 58 3e ba 63 33
40 9d 18 00 00 00 00 00 00 f0 02 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 57 64 81 a9 00 d9 14 34 01 88 13 00 00 22 f1 9b 41 95 0e 44 3e d8 7d 30
40 9f 18 00 00 00 00 00 00 75 01 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 8e 10 64 81 a9 00 d9 14 34 01 7c 15 00 00 30 07 99 41 79 df 3f 3e 99 31 2d
40 a0 18 00 00 00 00 00 00 bb 03 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 f5 37 64 81 a9 00 d9 14 34 01 10 27 00 00 33 83 98 41 e0 78 3a 3e 37 9c 2c
40 a2 18 00 00 00 00 00 00 12 02 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 13 f5 64 81 a9 00 d9 14 34 01 04 29 00 00 c2 87 94 41 0e 4a 3a 3e 6f 1a 28
40 a4 18 00 00 00 00 00 00 72 00 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 db b0 64 81 a9 00 d9 14 34 01 f8 2a 00 00 9b 16 95 41 88 3c 38 3e 1c bc 28
40 a5 18 00 00 00 00 00 00 9b 02 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 3b 54 64 81 a9 00 d9 14 34 01 ec 2c 00 00 e1 12 99 41 84 8f 36 3e d4 3e 2d
40 a7 18 00 00 00 00 00 00 e5 00 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 e0 72 64 81 a9 00 d9 14 34 01 e0 2e 00 00 fa 03 98 41 d2 b3 36 3e 3b 0c 2c
40 a8 18 00 00 00 00 00 00 5b 03 00 00 05 05 00 00 00 00 00 00 00 00 32 00 00 00 00 00 00 00 00
        """.trimIndent())))
        Thread.sleep(2 * 1000)
    }

    @Test
    fun storeRtuLog() {
        val tcp = KenteTcpHandler(dataMapper!!, rtuMapper!!, firmId, firmCode)
        val ch = EmbeddedChannel(KenteDecoder(), tcp)

        ch.writeInbound(Unpooled.buffer().writeBytes(BcdString.toByteArray("57 41 49 54 43 4f 4d 4d 2c 47 50 52 53 49 44 3d 32 30 31 30 36 31 31 30 2c")))
        Thread.sleep(2 * 1000)
    }
}